package gwtool.gui.login;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.KeyEvent;

import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

public class LoginPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4071847737236573173L;

	private JLabel lblEmail;

	private JTextField emailTextField;

	private JLabel lblPasswort;

	private JPasswordField passwordField;

	private JPanel panel;

	private JButton btnLogin;

	private JButton btnSchliessen;

	private LoginController loginController;

	public LoginPanel(LoginController loginController) {

		this.loginController = loginController;
		initialize();
	}

	private void initialize() {

		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 24, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 14, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 1.0, 0.0, 1.0, Double.MIN_VALUE };
		setLayout(gridBagLayout);
		GridBagConstraints gbc_lblEmail = new GridBagConstraints();
		gbc_lblEmail.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmail.anchor = GridBagConstraints.SOUTHEAST;
		gbc_lblEmail.gridx = 0;
		gbc_lblEmail.gridy = 0;
		add(getLblEmail(), gbc_lblEmail);
		GridBagConstraints gbc_emailTextField = new GridBagConstraints();
		gbc_emailTextField.anchor = GridBagConstraints.SOUTH;
		gbc_emailTextField.insets = new Insets(0, 0, 5, 0);
		gbc_emailTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_emailTextField.gridx = 1;
		gbc_emailTextField.gridy = 0;
		add(getEmailTextField(), gbc_emailTextField);
		GridBagConstraints gbc_lblPasswort = new GridBagConstraints();
		gbc_lblPasswort.anchor = GridBagConstraints.EAST;
		gbc_lblPasswort.insets = new Insets(0, 0, 5, 5);
		gbc_lblPasswort.gridx = 0;
		gbc_lblPasswort.gridy = 1;
		add(getLblPasswort(), gbc_lblPasswort);
		GridBagConstraints gbc_passwordField = new GridBagConstraints();
		gbc_passwordField.insets = new Insets(0, 0, 5, 0);
		gbc_passwordField.fill = GridBagConstraints.HORIZONTAL;
		gbc_passwordField.gridx = 1;
		gbc_passwordField.gridy = 1;
		add(getPasswordField(), gbc_passwordField);
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.gridwidth = 2;
		gbc_panel.insets = new Insets(0, 0, 0, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 2;
		add(getPanel(), gbc_panel);
		setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[] { getEmailTextField(), getPasswordField(),
				getBtnLogin(), getLblEmail(), getLblPasswort(), getPanel(), getBtnSchliessen() }));
	}

	private JLabel getLblEmail() {

		if (lblEmail == null) {
			lblEmail = new JLabel("Email");
		}
		return lblEmail;
	}

	public JTextField getEmailTextField() {

		if (emailTextField == null) {
			emailTextField = new JTextField();
			emailTextField.setColumns(10);
		}
		return emailTextField;
	}

	private JLabel getLblPasswort() {

		if (lblPasswort == null) {
			lblPasswort = new JLabel("Passwort");
		}
		return lblPasswort;
	}

	public JPasswordField getPasswordField() {

		if (passwordField == null) {
			passwordField = new JPasswordField();
			passwordField.addActionListener(loginController);
		}
		return passwordField;
	}

	private JPanel getPanel() {

		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
			panel.add(getBtnLogin());
			panel.add(getBtnSchliessen());
		}
		return panel;
	}

	public JButton getBtnLogin() {

		if (btnLogin == null) {
			btnLogin = new JButton("Login");
			InputMap inmap = btnLogin.getInputMap();
			KeyStroke enterPressed = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
			KeyStroke enterReleased = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
			inmap.put(enterPressed, "pressed");
			inmap.put(enterReleased, "released");
			btnLogin.setInputMap(WHEN_FOCUSED, inmap);
			btnLogin.addActionListener(loginController);
		}
		return btnLogin;
	}

	public JButton getBtnSchliessen() {

		if (btnSchliessen == null) {
			btnSchliessen = new JButton("schließen");
			btnSchliessen.addActionListener(loginController);
		}
		return btnSchliessen;
	}
}
